﻿@model TN_CMS.Domain.Entities.Product

@using TN_CMS.Admin.Helper.Common
@using TN_CMS.Framework.Variable

@{
    Layout = "~/Views/Shared/_Admin.cshtml";
}

@Html.Partial("_ProductHeader")
<div id="admin-container">
    <div class="tool-bar">
        <button class="blue" onclick="AdminHelper.LoadDetailData(-1);">Create</button>
        <button class="normal" onclick="AdminHelper.LoadDetailData();">Edit</button>
        <button class="normal" onclick="AdminHelper.UpdateActiveData(true);">Active</button>
        <button class="normal" onclick="AdminHelper.UpdateActiveData(false);">Inactive</button>
        <button class="red" onclick="AdminHelper.DeleteData();">Delete</button>
    </div>
    @Html.Partial("_AdminTable")
</div>
<div id="dialog-model" title="Manage Product" style="display:none">
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
        <div class="sale-setup-label-container">@Html.LabelFor(m => m.LstGroup)</div>
        <div class="sale-setup-input-container">
            @Html.DropDownListFor(m => m.LstGroup,
                (SelectList)ViewBag.LstGroup,
                new Dictionary<string, object> {
                    { "multiple", string.Empty },
                    { "style", "width:600px" }
                })
        </div>
        <div style="clear:both;height:10px;font-size:0px"></div>
    }
</div>

<script type='text/javascript'>
    AdminHelper = new MultiAdminHelper();

    AdminHelper.ReformatJsonData = function (detail) {
        var self = this;

        $("#LstGroup").val(detail.LstGroup).select2();
    }

    // override
    AdminHelper.GetCustomManageData = function () {
        var self = this;

        var unmappedViewModel = ko.mapping.toJS(self.ViewModel);
        unmappedViewModel.Description = CKEDITOR.instances.Description.getData();
        self.ViewModel.Description(unmappedViewModel.Description);

        unmappedViewModel.LstGroup = $("#LstGroup").val();

        return unmappedViewModel;
    }

    AdminHelper.DisplayCallBackSuccess = function (result) {
        var self = this;

        if (result.Message == null || result.Message == "") {
            self.Total = result.Optional;

            var data = new google.visualization.DataTable();

            data.addColumn('number', '@Html.DisplayNameFor(m => m.Id)' + CommonHelper.HiddenInputSorting("column0", "C.Id"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Name)' + CommonHelper.HiddenInputSorting("column1", "C.Name"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Summary)' + CommonHelper.HiddenInputSorting("column2", "C.Summary"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Price)' + CommonHelper.HiddenInputSorting("column3", "C.Price"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.CreatedOn)' + CommonHelper.HiddenInputSorting("column4", "C.CreatedOn"));
            data.addColumn('boolean', '@Html.DisplayNameFor(m => m.IsActive)' + CommonHelper.HiddenInputSorting("column5", "IsActive"));
            data.addColumn('string', '--' + CommonHelper.HiddenInputSorting("column6", "disable-sorting"));

            if (result.Result.length > 0) {
                for (var i = 0; i < result.Result.length; i++) {
                    var item = result.Result[i];

                    var cellName = "<span class='main-text'>" + item.Name + "</span>";
                    var cellPrice = item.Price.toString().digits();
                    item.CreatedOn = $.format.date(CommonHelper.ConvertJsonDate(item.CreatedOn), "@ConstantKeys.DATETIME_FORMAT_MMDDYY");
                    var btnRow = self.GetRowButton({
                        Id: item.Id,
                        Edit: true,
                        Delete: true
                    });

                    var tmpRow = [
                        item.Id,
                        cellName,
                        item.Summary,
                        cellPrice,
                        item.CreatedOn,
                        item.IsActive,
                        btnRow
                    ];

                    data.addRow(tmpRow);
                }
            }
            else {
                CommonHelper.ShowMsg(CommonPhrase.NoData);
            }

            self.Table = new google.visualization.Table($("#" + self.ContainerId + " .table-list")[0]);
            self.DrawTable(data);
        }
        else {
            CommonHelper.ShowMsg("Error: " + result.Message, MsgIconType.Error);
        }
    }

    AdminHelper.LoadDetailUrl = "@Url.Action("GetDetailProduct")";
    AdminHelper.ManageUrl = "@Url.Action("ManageProduct")";
    AdminHelper.UpdateActiveUrl = "@Url.Action("UpdateActiveProduct")";
    AdminHelper.DeleteUrl = "@Url.Action("DeleteProduct")";
    AdminHelper.CustomManage = true;
    AdminHelper.CanSort = true;
    AdminHelper.SortColumn = 0;
    AdminHelper.SortColumnName = "C.Id";
    AdminHelper.DialogWidth = 950;
    AdminHelper.DialogPosition = ["middle", 10];
    AdminHelper.Init("@Url.Action("GetListProduct")", "@AdminHelper.GetPageSize()");

    CommonHelper.SetHeadingTitle("Product Management");
</script>